//
//  MeViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/17.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "MeViewController.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UIImageView *topView;
@property (strong, nonatomic) UIButton *avatarButton;
@property (strong, nonatomic) UILabel *nicknameLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *listArray;
@property (nonatomic, strong) NSArray *imageListArray;


@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)refreshBaseControlValue {
    [self hideNavigationBottomLine];
//    [self setRightButtonImage:[UIImage imageNamed:@"icon_set_nor"]];
    

    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(px1920Height(566));
    }];
    
    [self.avatarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView).with.offset(px1920Height(184));
        make.centerX.equalTo(self.topView);
        make.height.width.mas_equalTo(px1080Width(206));
    }];

    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarButton.mas_bottom).with.offset(px1920Height(20));
        make.width.equalTo(self.topView);
        make.height.mas_equalTo(px1920Height(50));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.width.equalTo(self.topView);
        make.bottom.equalTo(self.view);
//        make.height.mas_equalTo(px1920Height(<#px#>));
    }];
    
    [self.view bringSubviewToFront:self.navigationBar];

}

#pragma -- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.listArray[section];
    return array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.listArray.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(168);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return px1920Height(40);
    } else if (section == 1){
        return px1920Height(100);
    }
    return px1920Height(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor darkGrayColor];
        label.textAlignment = NSTextAlignmentCenter;
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        label.text = [NSString stringWithFormat:@"Version %@",app_Version];
        return label;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * identifier = @"meCell";
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    if (indexPath.row != 3) {
        //右侧小箭头
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.textLabel.text = self.listArray[indexPath.section][indexPath.row];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:px1080Width(42)];
    cell.imageView.image = [UIImage imageNamed:self.imageListArray[indexPath.section][indexPath.row]];
//    if (indexPath.row == 3) {
//        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
//        cell.detailTextLabel.text = [NSString stringWithFormat:@"Version %@",app_Version];
//        cell.detailTextLabel.font = [UIFont systemFontOfSize:px1080Width(42)];
//    }
//    cell.imageView.image =

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.hidesBottomBarWhenPushed = YES;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            NSMutableArray *data = [[NSMutableArray alloc] init];
            [data addObjectsFromArray:[[DTCBluetoothDataBaseManager sharedInstance]getAllDevice]];
            if (data.count == 0) {
                [SVProgressHUD showErrorWithStatus:@"没有连接设备哦"];
            } else {
                MyMachineViewController *vc = [[MyMachineViewController alloc] init];
                vc.device = data[0];
                [self.navigationController pushViewController:vc animated:YES];
            }
        } else if (indexPath.row == 1) {
            NewsViewController *vc = [[NewsViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            WebViewController *vc = [[WebViewController alloc]init];
            vc.title = @"使用帮助";
            vc.type = 3;
            [self.navigationController pushViewController:vc animated:YES];

        } else if (indexPath.row == 1) {
            UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Suggestion" bundle:nil];
            SuggestionViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"SuggestionVC"];
            [self.navigationController pushViewController:vc animated:YES];
        } else if (indexPath.row == 2) {
            WebViewController *vc = [[WebViewController alloc]init];
            vc.title = @"关于我们";
            vc.type = 1;
            [self.navigationController pushViewController:vc animated:YES];
//        } else if (indexPath.row == 3) {
//            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
//            UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"当前版本号" message:app_Version delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alterView show];
        }
    }
    

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    self.hidesBottomBarWhenPushed = NO;

}

#pragma mark - 点击事件
- (void)onRightButtonClick:(id)sender
{
    //    MyAppointmentViewController *vc = [[MyAppointmentViewController alloc] init];
//    DeviceSettingViewController *vc = [[DeviceSettingViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void) didClickAvatarButton {
    self.hidesBottomBarWhenPushed = YES;
    UserViewController *vc = [[UserViewController alloc] init];
    [vc setUpdateBlock:^{
        [_avatarButton sd_setImageWithURL:[NSURL URLWithString:DTCEXRACT_OBJECT(IMAGE)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"photo_user1"]];
        _nicknameLabel.text = DTCEXRACT_OBJECT(NICKNAME);

    }];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}


#pragma mark -- 懒加载
- (UIImageView *)topView
{
    if(!_topView)
    {
        UIView *superView = self.view;
        _topView = [[UIImageView alloc]init];
        [superView addSubview:_topView];
        _topView.image = [UIImage imageNamed:@"bg_me"];
        _topView.userInteractionEnabled = YES;
    }
    return _topView;
}


- (UIButton *)avatarButton
{
    if(!_avatarButton)
    {
        _avatarButton = [[UIButton alloc]init];
        _avatarButton.layer.masksToBounds = YES;
        _avatarButton.layer.cornerRadius = px1080Width(206) / 2 ;
        [_avatarButton setBackgroundImage:[UIImage imageNamed:@"me_icon_user"] forState:UIControlStateNormal];
        [_avatarButton sd_setImageWithURL:[NSURL URLWithString:DTCEXRACT_OBJECT(IMAGE)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"photo_user1"]];
        [_avatarButton addTarget:self action:@selector(didClickAvatarButton) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:_avatarButton];
    }
    return _avatarButton;
}

- (UILabel *)nicknameLabel
{
    if(!_nicknameLabel)
    {
        UIView *superView = self.topView;
        _nicknameLabel = [[UILabel alloc]init];
        [superView addSubview:_nicknameLabel];
        _nicknameLabel.textAlignment = NSTextAlignmentCenter;
        [_nicknameLabel setFont:[UIFont systemFontOfSize:px1080Width(36)]];
        [_nicknameLabel setTextColor:[UIColor whiteColor]];
        _nicknameLabel.text = DTCEXRACT_OBJECT(NICKNAME);
    }
    return _nicknameLabel;
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        [superView addSubview:_tableView];
    }
    return _tableView;
}


- (NSArray *)listArray
{
    if(!_listArray)
    {
        _listArray = @[@[@"我的设备",@"我的消息"],@[@"使用帮助",@"意见反馈",@"关于我们"]];

    }
    return _listArray;
}
- (NSArray *)imageListArray
{
    if(!_imageListArray)
    {
        _imageListArray = @[@[@"icon_equipment",@"icon_news"],@[@"icon_help",@"icon_feedback",@"icon_our"]];
    }
    return _imageListArray;
}


@end
